import QtQuick
import QtQuick.Window
import QtQuick.Controls

Window {
  id: root
  visible: true
  width: 640
  height: 480
  title: qsTr("a04")
  Button {
    id: btn
    text: "测试自定义信号"
    background: Rectangle {
      anchors.fill: parent
      color: "blue"
    }
    // 定义信号
    signal mySignal
    // 定义信号（带参数）
    signal mySignalParam(int yOfs, string color)
    onClicked: {
      // 发送信号
      mySignal();
      mySignalParam(30, "yellow");
    }
    // 定义槽函数. 注意不加 function
    onMySignal: {
      btn.text = "MySignal";
    }
    // 注意带参数的槽函数格式
    onMySignalParam: (i, c) => {
      btn.y = i;
      btn.background.color = c;
    }
  }
  Rectangle {
    x: 100
    width: 100
    height: 100
    color: "red"
    property int size: 1
    onSizeChanged: {
      width = width + size * 10;
      height = height + size * 10;
    }
    onWidthChanged: {
      height = width;
    }
    Button {
      text: "+"
      width: 40
      height: 20
      onClicked: parent.size++
    }
    Button {
      x: 60
      text: "-"
      width: 40
      height: 20
      onClicked: parent.width -= 10
    }
  }
  Item {
    y: 100
    Rectangle {
      id: rect
      width: 100
      height: 100
      color: "#cecece"
    }
    // 定义信号
    signal itemSignal
    // 定义槽函数
    function itemSignalSlots() {
      rect.width += 10;
    }
    Button {
      text: "绑定槽函数"
      onClicked: {
        parent.itemSignal.connect(parent.itemSignalSlots);
      }
    }
    Button {
      y: 40
      text: "发送信号"
      onClicked: {
        parent.itemSignal();
      }
    }
    Button {
      y: 80
      text: "解绑"
      onClicked: {
        parent.itemSignal.disconnect(parent.itemSignalSlots);
      }
    }
  }
}
